package com.fourth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.UbBusinessInfoDao;
import com.fourth.dao.UbCommentDao;
import com.fourth.dao.UbCollectionDao;
import com.fourth.dao.UbGoodsDao;
import com.fourth.service.UbCollectionService;
import com.fourth.util.GetUidUtil;
import com.ljx.entity.BusinessInfo;
import com.ljx.entity.Collection;
import com.ljx.entity.Goods;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/3/2 17:14
 **/
@Service
public class UbCollectionServiceImpl implements UbCollectionService {
    @Resource
    private UbCollectionDao ubCollectionDao;

    @Resource
    private UbCommentDao ubCommentDao;

    @Resource
    private UbGoodsDao ubGoodsDao;

    @Resource
    private UbBusinessInfoDao ubBusinessInfoDao;

    @Override
    public int changeStatus(String id, Integer status, Integer type) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();

        if (type == 0){
            wrapper.eq("bid",id);
        } else {
            wrapper.eq("goodsid",id);
        }

        String uid = GetUidUtil.getUid();
        wrapper.eq("uid",uid);

        Collection collectionOne = ubCollectionDao.selectOne(wrapper);

        if (collectionOne == null){
            Collection collection = new Collection();
            collection.setStatus(type);
            collection.setIsDeleted(0);
            if (type == 0) {
                collection.setBid(id);
            } else {
                collection.setGoodsid(id);
            }
            collection.setUid(uid);
            return ubCollectionDao.insert(collection);
        }

        collectionOne.setIsDeleted(status);
        return ubCollectionDao.updateById(collectionOne);
    }

    @Override
    public int isCollectionBusiness(String id, Integer type) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        if (type == 0){
            wrapper.eq("bid",id);
        } else {
            wrapper.eq("goodsid",id);
        }
        wrapper.eq("is_deleted",0);

        String uid = GetUidUtil.getUid();
        wrapper.eq("uid",uid);

        Collection collection = ubCollectionDao.selectOne(wrapper);

        if (collection == null){
            return 1;
        }
        return 0;
    }

    @Override
    public List findCollection(Integer status) {
        String uid = GetUidUtil.getUid();
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        wrapper.eq("status",status);
        wrapper.eq("is_deleted",0);
        List<Collection> collections = ubCollectionDao.selectList(wrapper);
        Iterator<Collection> iterator = collections.iterator();

        if (status == 1){
            List<Goods> goodsList = new ArrayList<>(64);
            while (iterator.hasNext()){
                Collection collection = iterator.next();
                String goodsid = collection.getGoodsid();
                Goods goods = ubGoodsDao.findById(Integer.parseInt(goodsid));
                if (goods == null){
                    break;
                }
                goodsList.add(goods);
            }
            return goodsList;
        }

        List<BusinessInfo> businessInfos = new ArrayList<>(64);
        while (iterator.hasNext()){
            Collection collection = iterator.next();
            String bid = collection.getBid();
            BusinessInfo businessInfo = ubBusinessInfoDao.selectByBid(bid);
            if (businessInfo == null){
                break;
            }
            businessInfos.add(businessInfo);
        }
        return businessInfos;
    }

    @Override
    public int deleteCollection(String id, int status) {
        String uid = GetUidUtil.getUid();
        if (status == 1){
            return ubCollectionDao.setDeleteGid(uid,id);
        }

        return ubCollectionDao.setDeleteBid(uid,id);
    }
}
